
package br.ufu.lsi.database.factory;

import java.util.Date;

import twitter4j.RateLimitStatus;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterBuilder {

    public AtomicCounter counter = new AtomicCounter();

    public static TwitterKey[] twitterKeys = new TwitterKey[ 16 ];

    {
        /*//fabs177
        twitterKeys[ 0 ] = new TwitterKey( "6mqDzG5t39FNLsmPVWVP0YSP9", "lyMNt92GsIGoFavZDmVy2HJGXy8pJwohBl3au0viKCaJudGDbp", "991826522-plDPr6DvkSbjGRnnS4y7GgNj5I2Mb7pL8wTI36Ly", "xEnqU1Ay1m6PLwGGz2gsRIkP3hEdzcRnka7xzl0BEI9xh" );
        twitterKeys[ 1 ] = new TwitterKey( "gbjF8G0S1vneEtDNv98qDXQwg", "HJ6KI3hyxAwAVBQztj8cgoSi1p9LXEEMpHMR9sJLWEWdRESJiM", "991826522-yzEGL2PgUIUkuFg6fgbD3ZSjMPcXUnXU4mx8GRiO", "JiXhn5I8wrGA4OczUS2iZr0rp6qGRZkPCThLTetgIYIEu" );
        twitterKeys[ 2 ] = new TwitterKey( "NrzpbWV8a1JABVxBzIFel0c8Q", "gkli56moLEF9JBdWHwNPzuwWKrriFLlqCCvgzRsTc2DwAdFPAA", "991826522-sx5gYvHOaJr5HsFcpFGSSAxxbCKMSphzoGLOGa5x", "a6cIzuzN0t8Mujf8m46ogB0iacsPcN1gWGcBf9XwtKkoL" );
        twitterKeys[ 3 ] = new TwitterKey( "rH9NY6OqqXA4BgtHyoN9DcPTj", "TF9PTszCtdOXL75I8K74h8RIcvsHcq1I7IXSnQaiPDOwPMB5hL", "991826522-h3d2iTNSaF0QsAYuy81DF0566K17Vqb6lh1HH4UL", "7I2Uqa1hW06LJAQgH6OSDCLxc5Va6FxvTAnmCWkj55bq5" );
        twitterKeys[ 4 ] = new TwitterKey( "1EFJ89dlytTcSgPNNh1sbxCDE", "KKAymR8zaPK7nfx3FHwJKDaLsloKMtJRCpWtBdpC9I59Kqqz1L", "991826522-lu0cSH1hjz44lLr4WD81Z1MaVjJP0NurkSKby0iL", "AmOGARF53oE5ovS6bWfwiWTwaAjpwY6sT6a9QK2edwjfS" );
        twitterKeys[ 5 ] = new TwitterKey( "teTg2iiY9u3ahQet6zA1aK0Xj", "7DacedSSwqi1bv34Vpa9hOXZd9vr5q5REl5aKYYotuJypZ77Yw", "991826522-sgiVmCnxHlM7Plc04poIOvG09OUruL1feo6073yb", "0qDht8WuUwZxYjw80c9MDf02k0pM4JDlOu8GDAkXRPivg" );
        twitterKeys[ 6 ] = new TwitterKey( "a08k86Lokm9uEoLYN55nlejLL", "PkPdv4tCzMstB3YiewlHaX7JH48Uz80jtnZrq1d9SbJpqY4c6U", "991826522-666joHrYFTTef6XNgxSw3K3Z03mZ75kdWy0U4Nt6", "VWh6X0gEGOmkq9xIIR7rTrR1aayx42QwQ9dafQWwYtCKK" );
        twitterKeys[ 7 ] = new TwitterKey( "D0viCJCBnlyEZiFIkYbderhDm", "WBQgzVHmxOBs3ZNKo0nY9CaIWRy6hguxsjiKdY6tiYWxV8HPlO", "991826522-I1d816iC4PhYIWs280KOxS3jiiMOojOafjKKqlse", "KY75cG1NfnhkkTBzfRaH55LvXoSXQ1rIvLphrZJ6yqQt1" );
        twitterKeys[ 8 ] = new TwitterKey( "5lfAaxgFUFpkYhT4oUjZKevnw", "untwUOtvuz5RE1ZRvpWxH2G0SlNm8Z0E2rICUk17SRcDVvgQ1P", "991826522-EdEdlsis5Rq1naHwrvTAg8SwCoUmQ3topkdre46r", "WGIaoWyXRkGUzLBjGVPZNBpPrvlIkekozNlxgqIWcGHUP" );
        twitterKeys[ 9 ] = new TwitterKey( "zW2d5wqU4PEMYaDOcz2L27LsX", "wXx0OI9Mg0l1rPVIRjQmU3xSXUnw94XetnznPqLPJXIDV8Iwz5", "991826522-FkVcnR75XN5sn9MaEaoAqPVA8JNfBtz21dIrlPfu", "9239hZZh8BxEZmYMZvXrPHVLJmsdpuaCypwPCwUzuDseh" );
        twitterKeys[ 10 ] = new TwitterKey( "ITgAKVtdSwlpx7yBP1Wo6ou5l", "rln6kXnGMSN0mUWjP85Wg8JUGxfs2HE4B3bJovbzpH6O28iZQ7", "991826522-e71BQOsIAHAxkrfJuHj8LxnY6afTwmoFkAQXAhwU", "CHcuKNsq2XaT0o8typkMrX3c6Ha2EJqmtQh7mGgY3QpiI" );
        twitterKeys[ 11 ] = new TwitterKey( "Cwv2H8O9X5lIuIVgQBgIeY3WM", "XoKYza0NmcK89Xdo6FfdktAlf4xEpLmDESSfMdoi3cNWVNUb02", "991826522-qllu7yEoFmLYqTY4Y2sk3BgQcvGHmW0adLr4C6s7", "M9iiqaFrxHkTuG4LpQHLrYJhlvXcBJnQK7QY4Qjz0GXnc" );
        twitterKeys[ 12 ] = new TwitterKey( "15OiGjDz3cwzI3RWGugC0P0vz", "P0ro8NxyJqzRQXjNIxzpA2pzogzCyGM7orAvFz5x4KcONG90ne", "991826522-JwmPDxhw8ScU34XGWuDvnQR7k1W9WBZvzH2qXBf9", "52Dtg3aGdCUBgIXv4wAcrkyywz1dd7RbvltjVWoNvPbJP" );
        twitterKeys[ 13 ] = new TwitterKey( "rIo8Br7cyVS7dDwjEPW3sqtZR", "hUHyvFEEKeOmGxt5vZFFFvGVVQvhFz8tYs7lIxY4wTiUhOsDo2", "991826522-QKN2A3REyezT2CcUVnuMZ7ur5FOXNrpDxVrsKbav", "UCEeJ7ZcfCpZ87vpdcVtgJTDf7K2SDo3zNUiUdBI9wvz1" );
        
        //fabs177_2
        twitterKeys[ 14 ] = new TwitterKey( "JaqUSzqYtcCIhTYB827rqxCYE", "Kqhm3xgwl7Lz0cyBSx8OEYmEB6FeoUtKQkFUcMYJUcXWuVlqx4", "2966865743-8doOdeuUBwwxlfBiJOwtAYBq2cQrH1KE9zDJpEo", "PqkYV0Zq0TTNrrTiG09BqGlpzkU0v3cQsor8hTP7mlt9w" );
        twitterKeys[ 15 ] = new TwitterKey( "7sw0cjGjXtscxXF1ZCpp5gNaz", "dbSeGVkjhcT8C4ehpMGhw8lcgnnY5JZxNoq8y6fm59IlL192PZ", "2966865743-LOy3Y18D4v2LW4tjoGsv00GIuX8Gp4StvQjC8OE", "VLgw1TuuboeRb9km4gduyDEHcWEuE9k01RuKDpQgMNvqU" );
        twitterKeys[ 16 ] = new TwitterKey( "0mtDM4ETRFp94X4MSpb45v99h", "AIb3eeT1JWZVwHGo3PPfG3jxbLB25lj3XFPGIa7tW0bZsuDyS2", "2966865743-NS2HJlPQdMAV3a1jTirwbDX3Jb4q8qcRAQgWk87", "E2BWs0D3fKmGcaZVtdOcYRnBfXC4OOAOtcvInWdZC6bGF" );
        twitterKeys[ 17 ] = new TwitterKey( "6FHbE0agxclJoE8v8vvRcyXNo", "rGrEUg9VCP5uH53Q84tKYfxKA3Zy9DEG6AbgCfi6J2N8qoCBFH", "2966865743-f50Ozyjg9jLL3htCvey5yvcS6nzG2SsfNAS96UQ", "bbosOpKcynABXmafTEX1EqgJhkb2rItRvJXLEUAvlMUEl" );
        twitterKeys[ 18 ] = new TwitterKey( "iMy6ePS1O9jHxZd09z9j1tARN", "blSMitW3jXeNZew8SjKkXOGEjep78hrQlKEa0vjtdm4YUSCLCq", "2966865743-eGWc4L6J9q3DKc9OuWgI45rKkbClcotoe5yTzWN", "yWPIgUjjxg5VCpBtxyj9DXom8TrA0zKxWJAVo57562vPs" );
        twitterKeys[ 19 ] = new TwitterKey( "8YWs3joGVN4rcBsIHuuc6cRK6", "E1u6Cu6yiqycFDvfdd3WKLsLxnT0NKDTSvTgCAygT0GtWAmnnt", "2966865743-UL7xVxdYwCGdcIiAKpQxWSHBCiTfLKHMMowTCLh", "IEZkV8w5gT1p2a2ciYQvNBYACNCNhBOvhMrqEEv6KrAyJ" );
        twitterKeys[ 20 ] = new TwitterKey( "fQj0H3nvjY1zbkgAmNOAPWa6i", "bN3ETEb8MrNiUmD7GxCCUq87YYMtH1DX4uqwOLJFH343GLLYCG", "2966865743-RB9xMMLtSmWrUuC9Vh89hoaCnUIi74H0vfe1eEf", "bZPsSq5jBjJfbSlsIZVCdgkZdluDWPp65q5ijnSDToH90" );
        twitterKeys[ 21 ] = new TwitterKey( "EW9jb9UJwh8wva2Yvi6jujrCS", "JgKEwCl3Gt4phKPw0OpVABCQXiIQPN3z8vAXa6jYBNECSpsY12", "2966865743-KWt024dOvHWq0tdCa0ELNpKHstC5klQJOKmnLVx", "JQ1Lbt6RG3RGIGaQVBIlnUqCupIEo55K3oLu9C814CIBk" );
        twitterKeys[ 22 ] = new TwitterKey( "dynOcAHkLsAwovfFAoZ4mzJeH", "mvxONVX7TyF21XbwlJGXnSoNBEqd75uCeZJ7sIryGeQa2DvQ6y", "2966865743-TaNRHGjH1GYVZ8bS43jzL3i1ahntOYfBwAcDNe6", "v7wxTG1BWf9kHy7g2NkwSLqlMuWiA7SxUevuDWXMAM1wl" );
        twitterKeys[ 23 ] = new TwitterKey( "cSpnlhkZXW9fPwcp2ZYzNKa31", "8fqLu3jx0QS0fVL6VYeCR26AsGtxP21UoiA3GSbQffPkoYj5HU", "2966865743-VPDMzlPSWgKpETMxLLx6M1wbmc2LonUclTC0dep", "8r6cURlh7kh0izGdsWMfjCayN7msJhaqYexobI3TQUmXr" );
        twitterKeys[ 24 ] = new TwitterKey( "2eM1Tu9CID0iqvLWAylq3HM9j", "OzsvDTlsuTj89T0aqHmvcS0NtqFyY9g5vFdDNcWDIhpIGNPOhw", "2966865743-xqqR7ujWLgXDUFuw73oKKTAj0IF3fiTW9MbA4ME", "O8971O508t4ImWtk1MJOoUXY1Xd7oylha2NU5nEeGDUzy" );
        twitterKeys[ 25 ] = new TwitterKey( "xaIXGN9YqMrvQ3aUX2A6WX7Ma", "9QrZf52xoJYEO0GBCxE60AZdoJJFcqqZsQw4pZxORfVCHvpqdj", "2966865743-Goq0anfT3uJSGjxXYgPWY3hx6n5NgonT6q7wY9i", "XbLYtw3TULxOCCEHIO0ikUJtudq1VMYqsw2EUXW9IHNQb" );
        twitterKeys[ 26 ] = new TwitterKey( "uQYkmxquqBgGlxyYH99b7ChsB", "cguwcBzf5DyplRNbaBC1zri2e87dtg3fv3fhr2nOY7ieIdudTS", "2966865743-MN25j4lmCk3hkdsodOIDudWLRMqPyH46qJpmKLw", "46mLQjkEnMDGZAAOjkBpkWiuWZEgioZKS6Wj8xC8FT6av" );
        twitterKeys[ 27 ] = new TwitterKey( "tKd7jbshGF7zE3PRvMvD413AS", "EczDi3RA3Je19o8OqSz9IutAdTQpslK3IOB0HLq3APPsZ0PPk7", "2966865743-rgnYOoQv3boAf3GwrLVWanh3Fs6kmgHYInJ0JrD", "zBKMiWtun4j5Ol6SQNmosljiCSAcvVtLLfMg4WAxtWSFI" );
        twitterKeys[ 28 ] = new TwitterKey( "5EC2HbBLUqWFiG5197oPtFnWU", "OzLIRMfwrx31UBGrUkz9OPkpPmnYGELE5ZBJwWyqmfCEMt66Nu", "2966865743-82jg18heSShtYpVGQwtKUDmP58G1tYJQLayx2Nc", "2WGYEgtWku0um4IVgglzwxwHEKWFYdcwNTY4WCRcoTM8r" );
        twitterKeys[ 29 ] = new TwitterKey( "XTgFaMa2LRqZXHdidrlLuOQx0", "uTPalIMtx9ZbriIPO6aXqZ2EPJiDe25mrOtWMUqW5pz2HvwRfI", "2966865743-XQBAj1Ahj0fuwwiuxVgs3VDxSQBaFaulX4FjGYe", "brKZkdfTBMgIIyVZUG8IbAxDQQzvK7zmYoDhHtknN2jyg" );
*/
        //fabs177_3                        
        twitterKeys[ 0 ] = new TwitterKey( "IdOG02MJzRlpwBxpIcXFGWDO7", "rPldvrQqB0iXAjqmU1aq9WJGhJw1IkRZOnlThzfX9aqmIdxuBp", "3092619287-CjEZrwkr1FWTMcDPjws77HXeJ0wrlTTNoq0CltP", "D4zLn6y0Dh39nZAyZQB1Fz4O5XoyFmb4DKr2AvVXMQ5XZ" );
        twitterKeys[ 1 ] = new TwitterKey( "1yKtJH59mLRmj9Z8cHZDDvkIY", "OksiLTh4ZgnbObv72blMlkkItR14IDfQufd9Rl2zxyyHlCZOyD", "3092619287-Xylc8BB6Zy28bMVBAM0KCs3kH5hT7BQLTSUwxJS", "nB6YaOqWMxDluufWXyG5jwE5mLdoVvaxvNaC6LMFPX6MU" );
        twitterKeys[ 2 ] = new TwitterKey( "5p2L0yokDb14Q9hj9kkBLZ3sK", "sBt4KIfRlngH7faU3GrVhfrClBqj6u7Nf7PD8HFZjsnYB05zhB", "3092619287-Rsj8NJmld0KWhwDwTDPHp1pnxKuLstY9joDauLU", "M6ugGGAHTzstwspMWuDUNUHdsMz0mPsJOhhOKAQ7LCCYr" );
        twitterKeys[ 3 ] = new TwitterKey( "tG0RvT6mQyS5r48Bh55t9Izj3", "6jThxPYtjgfPrjUWeVtQeOWseoJTfAzYCMFuKA1J8xKm6nZP8N", "3092619287-insaZLr7geUtYKDaHR5NcsBZ5BxxyzW65q0BYKP", "VyvBFBbGEdBwE0RmXDU9riN8EQTFsbuyQF3ghrAexI49d" );
        twitterKeys[ 4 ] = new TwitterKey( "ZcItXPlrZHrJpll46j7rylYvP", "Bn7jCnzj9VlcYlJR67c56CGbGS8dMLDo0dDo3VW2lolpqF615w", "3092619287-JUoIHR1iDXuJvBhm0CZ0fm4AF6xxtEmizkKQTFV", "x9ejVLiIrjsBrAluy8yfci10TUHMsaXPkANxuwb5eFX0U" );
        twitterKeys[ 5 ] = new TwitterKey( "35CuoarokZK3IApvsKmbZmoPZ", "93JOVUQkTQKMo0QXtl0zh98EBt1oZYrIZW5WqQgsL8ytp41leR", "3092619287-QU3AIsKoUjvIFzDJi3wz08V050vQpN4R7uGoRwb", "pcCcYdgGH8JQmOA2ozelRiUTwaYwuCSCyCNfWBDRGBLto" );
        twitterKeys[ 6 ] = new TwitterKey( "NJfpiwL0zkD0n3pHLfjzrU2HQ", "bSfJ1xav5Pxlrk1oBi4O9RMHpsTB7rC50GxGP6gYhgo62nYVhc", "3092619287-NpSFiUqjafgIKD0HL7SZsHlUvzinFqXUsRjpK66", "7fA9aQaR4LsS23VIucE3Qkqb57XX9jlciYO3pHmTABRPr" );
        twitterKeys[ 7 ] = new TwitterKey( "Qrxw3GfL0dCnoQcDCmoarWMeE", "3aX2MjcXdyfklfMEBUo1cc3Rrh5DNFEeI56zCM6xp5T3m0qhwk", "3092619287-RYbtNLmUxo9HNioaQfNUqK9PCd9Ie5BcmHZb89P", "DORHXdtgZzUAojWg01iGViqmy6PuJkH5KdgvCHnhmmS7J" );
        twitterKeys[ 8 ] = new TwitterKey( "Z99eI0DSfMx9mmQ3pRDZWmBW5", "xUhZ2M2BXJHmjHR76MqPQsR4dKsVnvWUAKCczeAYjosRtIgfrS", "3092619287-P1ANg2VyI4Hc4RzSTSoGnqjOnC4yj6NUytn8APY", "MacVg2xpdqpA3dtfrKybHxXVmhG5O6Sb5YZY37Otk60Ki" );
        twitterKeys[ 9 ] = new TwitterKey( "hoJUy9LR3jlSWjWki6DywM8r4", "r9OguyydNbEPrVV53fXI8IbMPhvbHOXry2bqcH8iGUiZlMZU4r", "3092619287-WWVPTnKqhi1ikVi2rCRCNmpSCqkbor4rHdD6pox", "f1azKgGJWAogRw7Gmcak1Ur0CUZjUQ02WeVUhn8oApPf6" );
        twitterKeys[ 10 ] = new TwitterKey( "AnJ8Z4l5dW5RsAsb7gigVU5ZE", "DjRCDdF3hmWogq5B73h5Ktz8DpkQfsfaHZ26eQRXqP3ak5Bur5", "3092619287-1nDcDl3ObxSwNYxlYuYcvXlbfmKSttRUEFaYFLc", "mvnn43fuZ14mMf5t6kXS1eHVSsWfjzfX9tXFULRKJWtE0" );
        twitterKeys[ 11 ] = new TwitterKey( "wJqBzsmB7GMe5cL7tWwb4UVkc", "z82k3vGJINlTQkUZCTZVpJc0Ur1QnrWy5J7YSGQQ8yuThHBmnh", "3092619287-LFqBagZ3GubVqeKeLRS67DMY5uBLUj4qrH0M9TK", "p7NRzbcQq8xCdORF57Ev8bhxlci0pl4dxriK2b518y7qE" );
  
        //fabs177_4        
        twitterKeys[ 12 ] = new TwitterKey( "fbpn3LyNmI3ZvllXI77fJW4R7", "yghS4ObPDw0m82IhMRgTcrduCQx76DOGeuMH1d1CCXwFlr3o4I", "3097801347-zgm66R8AjXm8CerUKT1rv2K33K2VQVrPGFVFOW1", "pYeFB0kE1LjZFrDUNuyQtyThZxbwkpa89xoGrs71yTD42" );
        twitterKeys[ 13 ] = new TwitterKey( "UsQHHCPfs2S9ibfQssbGUgp7o", "fvQGuNrtQN3PILxnLoLdvV0EzcXRPBoBZysX7bgZtsltbRB9oo", "3097801347-TKVjgJscjLhkGdZNCq7tP5a2DkFC0auM5kjr8gU", "aEPcos0z5Ix8uEFu5sbfTe5UcQxTOSUDqK98GQug4XTeT" );
        twitterKeys[ 14 ] = new TwitterKey( "tKzUtjfgWrDrCD1bKlqBS7WPK", "AIT0MrcMMy5hIESzC1z8fffzYOAiWY3rI0X8DBLQ6vLJ0JqE5l", "3097801347-0FATmSmATktwnxiWJnRpYRSeNHL94qb9kasPoOj", "vqmamRih6xbEsdA7S1FAyWCSj6FInwYAc4f3c6fIoCTTX" );
        twitterKeys[ 15 ] = new TwitterKey( "l1SSEz4wm5oomlwR2rlr0AHBR", "Vm5KGAUXe0SxbvpZy9nBW18lncN9tVXVt0EOfr9tHDsrV9Yfhi", "3097801347-j74WBPlsdW7aeldZ0PJosS4ExvBV587isD452jU", "aBZb2qfTj0D4gpV0Efe6BAMYgIrt7bY61p3ThoqLUMgA7" );
    }
    

    public synchronized Twitter getTwitter() {

        int c = counter.value();
        
        String TWITTER_CONSUMER_KEY = twitterKeys[ c ].getConsumerKey();
        String TWITTER_SECRET_KEY = twitterKeys[ c ].getConsumerSecret();
        String TWITTER_ACCESS_TOKEN = twitterKeys[ c ].getAccessToken();
        String TWITTER_ACCESS_TOKEN_SECRET = twitterKeys[ c ].getAccessTokenSecret();

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled( true ).setOAuthConsumerKey( TWITTER_CONSUMER_KEY )
                .setOAuthConsumerSecret( TWITTER_SECRET_KEY )
                .setOAuthAccessToken( TWITTER_ACCESS_TOKEN )
                .setOAuthAccessTokenSecret( TWITTER_ACCESS_TOKEN_SECRET )
                .setPrettyDebugEnabled( true )
                .setJSONStoreEnabled( true );

        TwitterFactory tf = new TwitterFactory( cb.build() );
        Twitter twitter = tf.getInstance();
        
        return twitter;
    }
    
    public synchronized void updateTokens( Twitter twitter, String keyStatus ) throws TwitterException, InterruptedException {
        
        int c = counter.value();
        
        TwitterKey key = TwitterBuilder.twitterKeys[c];
        twitter.setOAuthConsumer( key.getConsumerKey(), key.getConsumerSecret() );
        twitter.setOAuthAccessToken( new AccessToken( key.getAccessToken(), key.getAccessTokenSecret() ) );
        
        RateLimitStatus status = twitter.getRateLimitStatus().get( keyStatus );
        if( status.getRemaining() == 0 ) {
            
            System.out.print( "[" + new Date( System.currentTimeMillis() ) + "] " );
            System.out.println( "Sleeping thread " + Thread.currentThread().getName() + " during "
                    + (status.getSecondsUntilReset()+60) + "sec ( "
                    + twitter.getOAuthAccessToken().getToken() + " ) ..." );

            if ( status.getSecondsUntilReset() > 0 )
                Thread.sleep( status.getSecondsUntilReset() * 1000 + 60000 );
        }
        
    }
    
    

}
